package com.zlw.main.audioeffects.utils;

import android.os.Environment;

import com.zlw.main.audioeffects.base.MyApp;


public class FilePathUtils {
    public static final String SDROOTPATH= Environment.getExternalStorageDirectory().getPath();
    public static final String DOWNLOADPATH = SDROOTPATH+"/" + Environment.DIRECTORY_DOWNLOADS;
    public static final String MUSIC_DIR =  "/" + Environment.DIRECTORY_MUSIC;// + "/";
    public static final String MUSIC_DOWNLOADS_DIR = MUSIC_DIR + "/" + Environment.DIRECTORY_DOWNLOADS;
    public static final String MUSIC_DIR_TYPE = MUSIC_DIR + "/";
    public static final String LRC_PATH = MUSIC_DIR + "/lrc";
    public static final String PIC_PATH = MUSIC_DIR + "/.picture";
    public static final String ALBUM_PATH = PIC_PATH + "/album";
    public static final String SHOW_PATH = PIC_PATH + "/show";
    public static final String RADIO_PATH = PIC_PATH + "/radio";
    public static final String LOCKSCREEN_JUMP_PATH = PIC_PATH + "/lockscreen";
    public static final String SEARCH_SONGER_PATH = MUSIC_DIR + "/search";
    public static final String SONG_BOARD_PIC_PATH = MUSIC_DOWNLOADS_DIR + "/songList/picture/";
    public static final String TEMP_SONG_BOARD_PIC_PATH = MUSIC_DOWNLOADS_DIR + "/songList/tempDir/";
    public static final String BILL_BOARD_PIC_PATH = MUSIC_DOWNLOADS_DIR + "/topList/picture/";
    public static final String TEMP_BILL_BOARD_PIC_PATH = MUSIC_DOWNLOADS_DIR + "/topList/tempDir/";
    public static final String BANNER_PATH = MUSIC_DOWNLOADS_DIR + "/.banner/";
    public static final String AUDIO_PCM_PATH = MUSIC_DOWNLOADS_DIR + "/pcm/";
    public static final String SORT_PATH = MUSIC_DOWNLOADS_DIR + "/sort/";
    public static final String LIBRARYFACE_PATH = MUSIC_DOWNLOADS_DIR + "/libraryface/";
    public static final String ARTISTFLAG_PATH = MUSIC_DOWNLOADS_DIR + "/artistflag/";
    public static final String SINGER_PHOTO_PIC_TEMP_PATH = MUSIC_DOWNLOADS_DIR + "/onlineartist/.photo";
    public static final String SINGER_PHOTO_PIC_SAVE_PATH = MUSIC_DOWNLOADS_DIR + "/onlineartist/photo";
    public static final String MUSIC_PATH = Environment.getExternalStorageDirectory().getPath() + MUSIC_DOWNLOADS_DIR;
    public static final String POPWINDOW_ACTION = "com.android.music.popwindow.action";
    public static final String POPWINDOW_FILE_PATH = MUSIC_PATH + "/popw/";
    public static final String POPWINDOW_DOWNLOAD_PATH =  MUSIC_DIR + "/" + Environment.DIRECTORY_DOWNLOADS + "/popw/";
    public static final String WEBVIEW_DOWNLOAD_PATH =  MUSIC_DIR + "/" + Environment.DIRECTORY_DOWNLOADS + "/webview/";
    public static final String COVER_PATH = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/musicCover";
    public static final String AD_PATH = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/splashAd";
    public static final String AD_PIC_PATH = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/splashAd" + "/picCache";
    public static final String DTS_PATH = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/Dts"+"/DTSname.txt";
    public static final String DTS_DATA = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/Dts"+"/DTSdata.txt";
    public static final String DTS_TYPE = Environment.getExternalStorageDirectory().getPath() + MUSIC_DIR +"/Dts"+"/DTSType.txt";
    public static final String MV_BOARD_PIC_PATH = MUSIC_DOWNLOADS_DIR + "/mvlist/picture/";
    public static final String OLD_PATH = Environment.getExternalStorageDirectory().getPath()
                + "/Android/data/com.android.music";
    public static final String LRC_OLD_PATH = OLD_PATH + "/lrc";
    public static final String SONG_BOARD_PREFIX_NAME = "song_list_bg_small_";
    public static final String BILL_BOARD_ICON_PREFIX = "top_list_bg_new_";
    public static final String RECOMMEND_BOARD_PREFIX_NAME = "user_list_bg_";
    public static final String MP3 = ".mp3";
    public static final String LRC = ".lrc";
    public static final String JPG = ".jpg";
    public static final String PNG = ".png";
    public static final String BMP = ".bmp";
    public static final String SONG_CACHE_DIR = MUSIC_DIR + "/cache";
    
    private static String[] mVolPaths;
    
    

    public static String getArtistFlagPicPath(String name) {
        return SDROOTPATH+ "/" + ARTISTFLAG_PATH + name;
    }
    
    public static String getCorrectVolumePath() {
        String[] volPaths = getVolumePaths();
        String extDir = Environment.getExternalStorageDirectory().getPath();
        String cur = extDir;
        if(FileUtil.isSDCardSpaceEnough()) {
            return extDir;
        }
        for(int i=0; i < volPaths.length; i++) {
            if(!extDir.equals(volPaths[i])) {
                if(FileUtil.isSpaceEnoughByPath(volPaths[i])) {
                    cur = volPaths[i];
                    return cur;
                }
            }
        }
        return cur;
    }
    
    private static String[] getVolumePaths() {
        if(mVolPaths == null) {
            mVolPaths = FileUtil.getStorageVolumePaths(MyApp.getInstance());
        }
        return mVolPaths;
    }

    public static String getSongBoardPicRealPath(String name) {
        return SDROOTPATH+ "/" + SONG_BOARD_PIC_PATH + name;
    }
    
    public static String getTmpSongBoardPicRealPath(String name) {
        return SDROOTPATH+ "/" + TEMP_SONG_BOARD_PIC_PATH + name;
    }
    
    public static String getSongBoardPicPath() {
        return SDROOTPATH+ "/" + SONG_BOARD_PIC_PATH;
    }
    
    public static String getTmpSongBoardPicPath() {
        return SDROOTPATH+ "/" + TEMP_SONG_BOARD_PIC_PATH;
    }
    
    public static String getBillBoardPicRealPath(String name) {
        return SDROOTPATH+ "/" + BILL_BOARD_PIC_PATH + name;
    }
    
    public static String getBillBoardPicPath() {
        return SDROOTPATH+ "/" + BILL_BOARD_PIC_PATH;
    }
    
    public static String getTmpBillBoardPicPath() {
        return SDROOTPATH+ "/" + TEMP_BILL_BOARD_PIC_PATH;
    }
    
    public static String getNativeSongFilePath(String songname, String artist) {
        if(songname == null || songname.length() == 0) {
            return "";
        }
        if(songname.contains("/")) {
            songname = songname.replaceAll("/", ",");
        }
        if(artist != null && artist.contains("/")) {
            artist = artist.replaceAll("/", ",");
        }
        String name = MUSIC_DOWNLOADS_DIR + "/" + songname;
        if(artist != null && artist.length() != 0) {
            name += " - " + artist;
        }
        name += ".mp3";        
        return getCorrectPath(name);
    }
    
    public static String getCacheSongFilePath(long id) {
        String name = SONG_CACHE_DIR + "/" + id;
        return getCorrectPath(name);
    }

    public static String getPopApkFilePath(String apkname) {
        if(apkname == null || apkname.length() == 0) {
            return "";
        }
        String name = POPWINDOW_DOWNLOAD_PATH + "/" + apkname;
        name += ".apk";        
        return getCorrectPath(name);
    }
    public static String getFilePath(String songname) {
        if(songname == null || songname.length() == 0) {
            return "";
        }
        String name = POPWINDOW_DOWNLOAD_PATH + "/" + songname;
        name += ".apk";        
        return getCorrectPath(name);
    }
    public static String getPopFilePath() {
        return getCorrectVolumePath() + POPWINDOW_DOWNLOAD_PATH;
    }
    public static String getSearchPath() {
        return SDROOTPATH+ "/" + SEARCH_SONGER_PATH;
    }
    
    public static String getLrcPath() {
        return getCorrectVolumePath() + LRC_PATH;
    }
    
    public static String getPcmPath(){
        return getCorrectVolumePath() +AUDIO_PCM_PATH ;
    }
    
    public static String[] getPcmPaths() {
        String[] volPaths = getVolumePaths();
        if(volPaths == null ||volPaths.length ==0){
            return null;
        }
        String[] strs =new String[volPaths.length];
        for (int i = 0; i < volPaths.length; i++) {
            strs [i] = volPaths[i]+AUDIO_PCM_PATH;
        }
        return strs;
    }
    
    public static String getPicPath() {
        return SDROOTPATH+ "/" + PIC_PATH;
    }

    public static String getSortPath() {
        return getCorrectVolumePath() + SORT_PATH;
    }
    
    public static String getLibraryFacePath() {
        return getCorrectVolumePath() +LIBRARYFACE_PATH;
    }
    
    public static String getLrcFilePath(String artistName, String songName){
        String ext = LRC_PATH + "/" + artistName + "_" + songName + ".lrc";
        return getCorrectPath(ext);
    }
    
    public static String getOldLrcFilePath(String artistName, String songName){
        return LRC_OLD_PATH + "/" + artistName + "_" + songName + ".lrc";
    }
    
    public static String getBannerPicPath(String name) {
        return SDROOTPATH+ "/" + BANNER_PATH + name;
    }
    
    public static String getDownloadPath() {
        return getCorrectVolumePath() + MUSIC_DOWNLOADS_DIR;
    }
    
    public static String getSingerPath() {
        return getPicPath();
    }
    
    public static String getAlbumPath() {
        return SDROOTPATH+ "/" + ALBUM_PATH;
    }
    
    public static String getAlbumFilePath(String artistName, String albumName) {
        if (artistName == null || albumName == null
                || MusicStore.UNKNOWN_STRING.equals(artistName)
                || MusicStore.UNKNOWN_STRING.equals(albumName)) {
            return null;
        }
        String ext = ALBUM_PATH + "/" + artistName + "_" + albumName + JPG;
        return SDROOTPATH+ "/" + ext;
    }
    
    public static String getSmallAlbumFilePath(String artistName, String albumName) {
        if (artistName == null || albumName == null
                || MusicStore.UNKNOWN_STRING.equals(artistName)
                || MusicStore.UNKNOWN_STRING.equals(albumName)) {
            return null;
        }
        String ext = ALBUM_PATH + "/" + artistName + "_small_" + albumName + JPG;
        return SDROOTPATH+ "/" + ext;
    }
    
    public static String getPhotoTempFilePath(String artistName, int order) {
        if (artistName == null || MusicStore.UNKNOWN_STRING.equals(artistName)) {
            return null;
        }
        String ext = SINGER_PHOTO_PIC_TEMP_PATH + "/" + artistName + "_" + order + JPG;
        return SDROOTPATH+ "/" + ext;
    }
    
    public static String getWeiboPhotoTempFilePath(String name) {
        if (name == null || MusicStore.UNKNOWN_STRING.equals(name)) {
            return null;
        }
        String ext = SINGER_PHOTO_PIC_TEMP_PATH + "/" + name + JPG;
        return SDROOTPATH+ "/" +ext;
    }
    
    public static String getSingerPicPath(String artist){
        //String ext = PIC_PATH + "/" + artist + JPG;      
        return SDROOTPATH+ "/" + PIC_PATH + "/" + artist + JPG;
    }
    
    public static String getSingerSmallPicPath(String artist){
        return SDROOTPATH+ "/" + PIC_PATH + "/" + artist + "_small" + JPG;      
    }
    
    public static String getShowPicPath(String artist){
        return SDROOTPATH + "/" + SHOW_PATH + "/" + artist + JPG;
    }
    
    public static String getRadioPicPath(String content){
        return SDROOTPATH+ "/" + RADIO_PATH + "/" + content + JPG;
    }
    
    public static String getLockScreenJumpPicPath(String content) {
        return SDROOTPATH+ "/" + LOCKSCREEN_JUMP_PATH + "/" + content + JPG;
    }
    
    public static String getExistSingerPicPath(String artist){
        String artistPic = getSingerPicPath(artist);
        if(FileUtil.isExist(artistPic)){
            return artistPic;
        }
        return null;
    }
    
    public static String getExistSingerSmallPicPath(String artist){
        String artistPic = getSingerSmallPicPath(artist);
        if(FileUtil.isExist(artistPic)){
            return artistPic;
        }
        return null;
    }
    
    public static String getSingerPhotoTempPath() {
        return SDROOTPATH+ "/" + SINGER_PHOTO_PIC_TEMP_PATH;
    }
    
    public static String getSingerPhotoSavePath() {
        return SDROOTPATH+ "/" + SINGER_PHOTO_PIC_SAVE_PATH;
    }
    
    public static String getmvBoardPicRealPath(String artist, String title) {
        String ext = MV_BOARD_PIC_PATH + artist + "_" + title;
        return SDROOTPATH + "/" +ext;
    }
    
    private static String getCorrectPath(String name) {
        String[] volPaths = getVolumePaths();
        if(volPaths == null) {
            return null;
        }
        String path = null;
        for(int i = 0; i < volPaths.length; i++) {
            path = volPaths[i] + name;
            if(FileUtil.isExist(path)) {
                return path;
            }
        }
        return getCorrectVolumePath() + name;
    }
    
    public static boolean isEnoughSpace() {
        boolean isEnough = false;
        String[] volPaths = getVolumePaths();
        for(int i=0; i < volPaths.length; i++) {
            if(FileUtil.isSpaceEnoughByPath(volPaths[i])) {
                return true;
            }     
        }
        return isEnough;
    }
}
